package com.web.kdl.service.auth;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.web.kdl.common.result.CommonResult;
import com.web.kdl.daoV2.auth.PermissionDao;
import com.web.kdl.daoV2.auth.RolePermissionDao;
import com.web.kdl.daoV2.auth.UserRoleDao;
import com.web.kdl.entity.auth.PermissionInfo;
import com.web.kdl.entity.auth.RolePermission;
import com.web.kdl.entity.auth.UserRole;
import com.web.kdl.pojo.Vo.auth.PermissionVo;
import com.web.kdl.pojo.form.PermissionAddForm;
import com.web.kdl.pojo.form.PermissionUpdateForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @Author： shulibin
 * @Date： 2025/7/30 13:48
 * @Describe：
 */
@Service
public class PermissionServiceImpl implements PermissionService{
    @Resource
    PermissionDao permissionDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public List<PermissionInfo> getAllPermissions() {
        // 实现查询所有权限
        List<PermissionInfo> permissionInfos = permissionDao.selectList(null);
        return permissionInfos;
    }

    @Override
    public PermissionInfo getById(String id) {
        PermissionInfo permissionInfo = permissionDao.selectById(id);
        return permissionInfo;
    }

    @Override
    public CommonResult createPermission(PermissionAddForm form) {
        LambdaQueryWrapper<PermissionInfo> wapper = new LambdaQueryWrapper<>();
        wapper.eq(PermissionInfo::getPermissionCode, form.getPermissionCode()).or().eq(PermissionInfo::getPermissionName, form.getPermissionName());
        PermissionInfo existPer = permissionDao.selectOne(wapper);
        if (existPer != null) {
            return CommonResult.error("权限已存在");
        }
        PermissionInfo permissionInfo = new PermissionInfo();
        permissionInfo.setPermissionCode(form.getPermissionCode());
        permissionInfo.setPermissionName(form.getPermissionName());
        permissionInfo.setDescription(form.getDescription());
        permissionDao.insert(permissionInfo);
        return CommonResult.success("添加成功");
    }

    @Override
    public CommonResult updateById(String id, PermissionUpdateForm form) {
        PermissionInfo permissionInfo = permissionDao.selectById(id);
        if (permissionInfo == null){
            return CommonResult.error("权限不存在");
        }
        permissionInfo.setPermissionCode(form.getPermissionCode());
        permissionInfo.setPermissionName(form.getPermissionName());
        permissionInfo.setDescription(form.getDescription());
        permissionDao.updateById(permissionInfo);
        return CommonResult.success("权限更新成功");
    }

    @Override
    @Transactional
    public CommonResult removeById(String id) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getPermissionId, id);
        rolePermissionDao.delete(wrapper);
        int i = permissionDao.deleteById(id);
        return i>0?CommonResult.success("删除成功"):CommonResult.error("删除失败");
    }

    @Override
    public PermissionInfo getByCode(String permissionCode) {
        LambdaQueryWrapper<PermissionInfo> wapper = new LambdaQueryWrapper<>();
        wapper.eq(PermissionInfo::getPermissionCode, permissionCode);
        PermissionInfo permissionInfo = permissionDao.selectOne(wapper);
        return permissionInfo;
    }

    @Override
    public List<PermissionInfo> getUserPermissions(String userId) {
        UserRole userRole = userRoleDao.selectOne(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId));
        if (userRole != null) {
            List<PermissionInfo> permissions = permissionDao.selectList(new LambdaQueryWrapper<PermissionInfo>().eq(PermissionInfo::getId, userRole.getRoleId()));
            return permissions;
        }
        return Collections.emptyList();
    }

    @Override
    @Transactional
    public CommonResult removeBatchByIds(List<String> ids) {
        rolePermissionDao.delete(new QueryWrapper<RolePermission>().lambda().in(RolePermission::getRoleId, ids));
        int i = permissionDao.deleteBatchIds(ids);
        if (i ==ids.size()){
            return CommonResult.success("批量删除成功");
        }
        return CommonResult.error("批量删除失败");
    }
}
